<?php
define('HEADCHAR','Content-type:application/json; charset=utf-8');
require('common.php');
/*****************************************/
/*				 初始变量				 */
/*****************************************/
$m=getgpc('m');
$a=getgpc('a');
$m=empty($m)?'index':$m;
$a=empty($a)?'body':$a;
$isLogin=FALSE;
$uid=0;
$auth=getgpc('auth','R');
if (in_array($m,array('index','member','node'),TRUE)) {
	if(is_file('model/api/'.$m.'.php')) {
		require('model/api/'.$m.'.php');
	} else {
		exit(json_encode(array('success'=>0,'errcode'=404,'errmsg'=>'API Not Found')));
	}
	$m.='_model';
	$_ENV['HOME']['model']=$m::getInstance();
	$method='on'.$a;
	if(method_exists($_ENV['HOME']['model'],$method) && $a{0}!='_') {
		if (!empty($auth)) {
			$sql=getClass('mysql');
			$dbauth=$sql->GetOne('SELECT * FROM `#@__auth` WHERE auth = ?',array($auth));
			if (!empty($dbauth['overdue'])) { //Auth存在数据库中
				if (time()>$dbauth['overdue'])) { //Auth已过期
					$sql->Query('DELETE `#@__auth` WHERE auth = ?',array($auth));
				} else {
					$isLogin=TRUE;
					$uid=$dbauth['uid'];
				}
			}
		}
		$r=$_ENV['HOME']['model']->$method();
		if (!isset($r['errcode'])) {
			echo json_encode(array_merge(array('success'=>1),$r));
		} else {
			echo json_encode(array_merge(array('success'=>0),$r));
		}
	} else {
		exit(json_encode(array('success'=>0,'errcode'=>404,'errmsg'=>'API Not Found')));
	}
} else {
	exit(json_encode(array('success'=>0,'errcode'=>404,'errmsg'=>'API Not Found')));
}
?>